package com.android.view.fragment.info;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.util.ToastUtil;
import com.android.view.activity.user.LoginActivity;
import com.android.R;
import com.android.model.info.LocalSp;
import com.android.model.entity.res.UserInfoResult;
import com.android.presenter.info.QueryUserInfoPresenter;
import com.android.presenter.iview.IUserInfoView;
import com.android.view.activity.user.SettingActivity;
import com.android.view.activity.user.TradePWDVerifySecurityActivity;
import com.android.view.fragment.base.BaseFragment;
import com.android.view.widget.CommonHeaderView;

import butterknife.BindColor;
import butterknife.BindView;

/**
 * author:  TenFace
 * date:    2017/9/27
 * description:
 */

public class UserFragment extends BaseFragment implements CommonHeaderView.LayoutClickCallback, IUserInfoView {

    //    @BindView(R.id.srl_movie)
//    SwipeRefreshLayout mSrlMovie;
    @BindView(R.id.commonHeaderView)
    CommonHeaderView commonHeaderView;
    @BindView(R.id.llUserInfo)
    LinearLayout llUserInfo;
    @BindView(R.id.tradingPassword)
    LinearLayout tradingPassword;
    @BindView(R.id.userName)
    TextView userName;

    @BindColor(R.color.lite_blue)
    int colorBlue;
    @BindColor(R.color.green)
    int colorGreen;
    @BindColor(R.color.orange)
    int colorOrange;
    private QueryUserInfoPresenter queryUserInfoPresenter;
    private String userToken;

    public static UserFragment newInstance(int type) {
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        UserFragment fragment = new UserFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        userToken = LocalSp.getInstance(getContext()).getSSOToken();
        if (!TextUtils.isEmpty(userToken)) {
            userName.setText("张腾飞");
        } else {
            userName.setText("Please log in");
        }
    }

    @Override
    protected int setContentLayout() {
        return R.layout.fragment_user;
    }

    @Override
    protected void initView() {
        //设置下拉刷新的圆环变化颜色，蓝绿橙
//        mSrlMovie.setColorSchemeColors(colorBlue, colorGreen, colorOrange);
    }

    @Override
    protected void obtainData() {
        queryUserInfoPresenter = new QueryUserInfoPresenter(this);
    }

    @Override
    protected void initEvent() {
        llUserInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userToken = LocalSp.getInstance(getContext()).getSSOToken();
                if (!TextUtils.isEmpty(userToken)) {
                    ToastUtil.show("您已登录，请尽情享用我吧！");
                } else {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
        commonHeaderView.setClickCallback(this);
        tradingPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userToken = LocalSp.getInstance(getContext()).getSSOToken();
                if (!TextUtils.isEmpty(userToken)) {
                    queryUserInfoPresenter.getUserInfo(userToken);
                } else {
                    Intent intent = new Intent();
                    intent.setClass(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void rightBtn(View v) {
        Intent intent = new Intent();
        intent.setClass(getContext(), SettingActivity.class);
        startActivity(intent);
    }

    @Override
    public void queryUserInfoSuccess(UserInfoResult userInfoResult) {
        if (userInfoResult != null) {
            TradePWDVerifySecurityActivity.actionStart(getContext(), userInfoResult.getPhone(), userInfoResult.getEx55Pin());
        } else {
            ToastUtil.show("查询失败，请重试！");
        }
    }

    @Override
    public void queryUserInfoFail(int status, String desc) {
        ToastUtil.show("查询失败，" + desc);
    }
}
